<template>
  <!-- <div class="header">
   
  </div> -->
  <!-- 背景 -->
  <div class="bg-img-box">
    <div class="bg-img"></div>
  </div>
  <!-- 表单 -->
  <div class="box">
    <router-link to="StoreMain">
      <div class="logo"></div>
    </router-link>
    <div class="input-box">
      <label>账号</label>
      <input type="text" v-model="username" />
    </div>
    <div class="input-box">
      <label>密码</label>
      <input type="password" v-model="password" />
    </div>
    <div class="input-box CodeInp">
      <label>验证码 </label>
      <div>
        <input type="text" v-model="verifiCode" />
        <!-- 验证码 -->
        <span @click="refreshCode" class="identifyCode">
          <s-identify :identifyCode="identifyCode"></s-identify>
        </span>
      </div>
    </div>
    <div class="btn-box">
      <div>
        <button @click="login">登录</button>
      </div>
      <div class="span_footer">
        <router-link to="register">立即注册！</router-link>
        <a href="#">忘记密码?</a>
      </div>
    </div>
  </div>
</template>

<script>
import SIdentify from "@/components/identify.vue";
import { ref, getCurrentInstance } from "vue";
import { useStore } from "vuex";
import { useRouter } from "vue-router";
import { loginShop } from "@/api/api.js";
export default {
  name: "login",
  components: { SIdentify },
  setup() {
    //图片验证码
    const identifyCode = ref("");
    //用户输入的验证码
    const verifiCode = ref(); //验证码
    // 验证码规则
    const identifyCodes = "0123456789qwertyuiopasdfghjklzxcvbnm";
    // const identifyCodes = "1"; //测试用的验证码
    // 切换验证码
    function refreshCode() {
      identifyCode.value = "";
      makeCode(identifyCodes, 4);
    }
    //进入页面调用一次刷新验证码
    refreshCode();
    // 生成随机验证码
    function makeCode(o, l) {
      for (let i = 0; i < l; i++) {
        identifyCode.value +=
          identifyCodes[
            Math.floor(Math.random() * (identifyCodes.length - 0) + 0)
          ];
      }
    }
    //引入路由
    const router = useRouter();
    // 引入vuex
    const store = useStore();
    // 引入axios
    const { proxy } = getCurrentInstance();
    // 表单信息
    const username = ref("");
    const password = ref("");
    // 登录
    const login = () => {
      if (verifiCode.value == identifyCode.value) {
        // 登录
        loginShop("get", username.value,password.value).then((res) => {
          console.log(res.data);
          if (res.data.length == 0) {
            alert("此用户未注册");
          } else {

                let data = {
                  shopToken: "9734",
                  info: { username: res.data[0].username, id: res.data[0].id },
                };
                store.dispatch("setUserInfo", data).then(() => {
                  router.push("/StoreMain");
                });

            
          }
        });
      } else {
        alert("验证码错误");
        refreshCode();
      }
    };
    return {
      identifyCode, //图片验证码
      verifiCode, //用户输入的验证码
      username,
      password,
      refreshCode,
      login,
    };
  },
};
</script>

<style lang="scss" scoped>
// 模糊背景
.bg-img-box {
  display: flex;
  flex-direction: column;
  position: absolute;
  bottom: 0;
  top: 0;
  left: 0;
  right: 0;
  overflow: hidden;
}
.bg-img {
  width: 100%;
  height: 100%;
  background: url(@/assets/background.jpg);
  background-repeat: no-repeat;
  background-size: cover;
  filter: blur(20px);
  transform: scale(1.5);
  background-position: 50%;
}
.bg-img::after {
  content: " ";
  display: block;
  width: 100%;
  height: 400rpx;
  background: rgba(17, 17, 17, 0.2);
  z-index: 1;
}
// 登录
.box {
  width: 750px;
  height: 500px;
  margin: 0 auto;
  margin-top: 120px;
  border-top: 1px solid rgba(255, 255, 255, 0.5);
  border-left: 1px solid rgba(255, 255, 255, 0.5);
  border-bottom: 1px solid rgba(255, 255, 255, 0.3);
  border-right: 1px solid rgba(255, 255, 255, 0.3);
  backdrop-filter: blur(10px);
  background: rgba(50, 50, 50, 0.2);
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  border-radius: 10px;
}
// 验证码的输入框
.CodeInp {
  width: 250px;
  div {
    display: flex;
    justify-content: space-between;
    align-items: center;
  }
  input {
    width: 100px !important;
  }
}
.box > h2 {
  color: rgba(255, 255, 255, 0.9);
  margin-bottom: 30px;
}

.box .input-box {
  display: flex;
  flex-direction: column;
  box-sizing: border-box;
  margin-bottom: 10px;
}

.box .input-box label {
  font-size: 13px;
  color: rgba(255, 255, 255, 0.9);
  margin-bottom: 5px;
}

.box .input-box input {
  letter-spacing: 1px;
  font-size: 14px;
  box-sizing: border-box;
  width: 250px;
  height: 35px;
  border-radius: 5px;
  border: 1px solid rgba(255, 255, 255, 0.5);
  background: rgba(255, 255, 255, 0.2);
  outline: none;
  padding: 0 12px;
  color: rgba(255, 255, 255, 0.9);
  transition: 0.2s;
}

.box .input-box input:focus {
  border: 1px solid rgba(255, 255, 255, 0.8);
}

.box .btn-box {
  width: 250px;
  display: flex;
  flex-direction: column;
  align-items: start;
}

.span_footer > a {
  font-size: 13px;
  color: rgba(255, 255, 255, 0.7);
}

.span_footer > a:hover {
  color: rgba(255, 255, 255, 1);
}

.box .btn-box > div {
  margin-top: 10px;
  display: flex;
  justify-content: center;
  align-items: center;
  width: 100%;
}

.box .btn-box > div > button {
  outline: none;
  margin-top: 10px;
  display: block;
  font-size: 14px;
  border-radius: 5px;
  transition: 0.2s;
}

.box .btn-box > div > button:nth-of-type(1) {
  width: 120px;
  height: 35px;
  color: rgba(255, 255, 255, 0.9);
  border: 1px solid rgba(192, 119, 91, 0.7);
  background: rgba(156, 39, 176, 0.6);
  width: 70%;
}

.box .btn-box > div > button:hover {
  border: 1px solid rgba(251, 128, 71, 0.7);
  background: rgba(156, 39, 176, 1);
}
.span_footer {
  justify-content: space-between !important;
}
// 验证码
.identifyCode {
  width: 100px;
  height: 40px;
}
// logo
.logo {
  background: url(@/assets/logo_lr.png);
  height: 100px;
  width: 150px;
  background-position: center center;
  background-repeat: no-repeat;
  background-size: cover;
  margin-bottom: 30px;
}
</style>